<template>
  <div class="questionInfo">
    <!-- 头部导航 -->
    <sub-nav-bar
      path="/home/question"
      title="刷题"
      right="答题卡"
      @rightClick="rightClick"
    ></sub-nav-bar>
    <!-- 刷题内容 -->
    <div class="content" v-if="list.length && list[currIndex].detail">
      <div class="title">
        [{{ typeObj[list[currIndex].detail.type] }}]{{
          list[currIndex].detail.title
        }}
      </div>
      <!--  -->
      <van-tag
        class="tag"
        color="#f7f4f5"
        text-color="#c4c4cb"
        v-for="(item, index) in list[currIndex].detail.tag"
        :key="index"
        >{{ item }}</van-tag
      >
      <!-- 选择项 单选-->
      <ul class="select" v-if="typeObj[list[currIndex].detail.type] === '单选'">
        <li
          class="select-item iconfont"
          v-for="(item, index) in list[currIndex].detail.option"
          :key="index"
          @click="singleSelect(index)"
          :class="{
            active: ans1 === str[index],
            pass:
              list[currIndex].answer &&
              list[currIndex].answer.singleAnswer === str[index],
            error:
              list[currIndex].answer &&
              ans1 === str[index] &&
              list[currIndex].answer.singleAnswer !== ans1
          }"
        >
          {{ str[index] }}:{{ item }}
        </li>
      </ul>
      <!-- 选择项 多选-->
      <ul
        class="select"
        v-else-if="typeObj[list[currIndex].detail.type] === '多选'"
      >
        <li
          class="select-item iconfont"
          v-for="(item, index) in list[currIndex].detail.option"
          :key="index"
          @click="multiSelect(index)"
          :class="{
            active: ans2.includes(str[index]),
            pass:
              list[currIndex].answer &&
              list[currIndex].answer.multipleAnswer.includes(str[index])
          }"
        >
          {{ str[index] }}:{{ item }}
        </li>
      </ul>
      <!-- 简答输入框 -->
      <van-field
        v-else
        v-model="ans3"
        type="textarea"
        rows="5"
        class="field"
        @input="inputEvent"
        :disabled="step > 2"
      >
      </van-field>
      <!-- 答案解析 -->
      <div class="answer" v-if="step > 2">
        <h3 class="answer-title">
          答案解析
        </h3>
        <div class="right-answer">
          <span v-if="typeObj[list[currIndex].detail.type] === '单选'">
            正确答案:{{ list[currIndex].answer.singleAnswer }}
          </span>
          <span v-else-if="typeObj[list[currIndex].detail.type] === '多选'">
            正确答案:{{ list[currIndex].answer.multipleAnswer.join(',') }}
          </span>
        </div>
        <div class="other">
          <div class="i1">
            难度:{{ diffObj[list[currIndex].answer.difficulty] }}
          </div>
          <div class="i2">提交次数:{{ list[currIndex].answer.submitNum }}</div>
          <div class="i3">正确次数:{{ list[currIndex].answer.correctNum }}</div>
        </div>
        <div class="txt">
          {{ list[currIndex].answer.answerAnalysis }}
        </div>
      </div>
    </div>
    <div class="bottom">
      <div class="b1">
        <div class="iconfont">&#xe611;</div>
        <div class="txt">收藏</div>
      </div>
      <div class="b1">
        <div class="iconfont">&#xe606;</div>
        <div class="txt">反馈</div>
      </div>
      <div class="b2">
        <span>{{ currIndex + 1 }}</span
        >/{{ list.length }}
      </div>
      <van-button
        class="b3"
        :disabled="step === 1"
        @click="submitEvent"
        v-if="step < 3"
        >提交
      </van-button>
      <!-- 下一题 -->
      <van-button class="b3" v-else-if="step === 3" @click="next"
        >下一题</van-button
      >
      <van-button class="b3" v-else>结束</van-button>
    </div>
    <!-- 答题卡弹框 -->
    <van-popup v-model="showCard" position="bottom">
      <ul class="card">
        <li
          class="card-item"
          @click="skip(index)"
          :class="{
            'card-pass': item.answer && item.answer.isRight,
            'card-error': item.answer && !item.answer.isRight
          }"
          v-for="(item, index) in list"
          :key="index"
        >
          {{ index + 1 }}
        </li>
        <li class="placeholder"></li>
        <li class="placeholder"></li>
        <li class="placeholder"></li>
        <li class="placeholder"></li>
      </ul>
    </van-popup>
  </div>
</template>
<script>
import {
  interViewQuestions,
  questionsSubmit,
  questionsId
} from '@/api/question'
export default {
  name: 'questionInfo',
  data () {
    return {
      step: 1, // 步数
      id: this.$route.query.type, // 岗位
      city: this.$route.query.city, // 城市
      list: [],
      currIndex: 0,
      typeObj: { 1: '单选', 2: '多选', 3: '简答' },
      str: 'ABCDEFG',
      ans1: '', // 单选
      ans2: [], // 多选
      ans3: '', // 简单
      bol: true, // 静默刷新
      diffObj: { 1: '简单', 2: '一般', 3: '困难' },
      showCard: false // 答题卡弹框
    }
  },
  methods: {
    // 答题卡
    rightClick () {
      this.showCard = true
    },
    // 答题卡跳转到某一题
    async skip (index) {
      this.currIndex = index
      this.showCard = false
      // 清空答案
      this.ans1 = ''
      this.ans2 = []
      this.ans3 = ''
      // 没有题目处理
      if (!this.list[this.currIndex].detail) {
        // 获取题目
        const res = await questionsId(this.list[this.currIndex].id)
        this.$toast.clear()
        this.list[this.currIndex].detail = res.data.data
        this.step = 1
      } else {
        // 有答案处理
        if (this.list[this.currIndex].answer) {
          if (this.currIndex === this.list.length - 1) {
            this.step = 4
          } else {
            this.step = 3
          }
          this.ans1 = this.list[this.currIndex].ans1
          this.ans2 = this.list[this.currIndex].ans2
          this.ans3 = this.list[this.currIndex].ans3
        } else {
          // 没有答案处理
          this.step = 1
        }
      }
      this.bol = false
      this.$nextTick(() => {
        this.bol = true
      })
    },
    // 单选
    singleSelect (index) {
      // 限制选项点击事件
      if (this.step <= 2) {
        this.ans1 = this.str[index]
        // 修改步骤为2
        this.step = 2
        // window.console.log(this.ans1)
      }
    },
    // 多选
    multiSelect (index) {
      if (this.step <= 2) {
        // 查找答案中当前选项索引
        const _index = this.ans2.indexOf(this.str[index])
        // 当前选择的答案中有选择过该选项
        if (_index !== -1) {
          // 有选择过，第二次选择就删除
          this.ans2.splice(_index, 1)
        } else {
          // 没选择过就是新增
          this.ans2.push(this.str[index])
        }
        if (this.ans2.length > 0) {
          // 如果选择了答案step=2,提交按钮可以点击
          this.step = 2
        } else {
          // 没选择答案 提交按钮不可以点击
          this.step = 1
        }
        // window.console.log(this.ans2)
      }
    },
    // 简答
    inputEvent () {
      if (this.ans3.length > 0) {
        this.step = 2
      } else {
        this.step = 1
      }
    },
    // 提交
    async submitEvent () {
      this.$toast.loading({ duration: 0 })
      const res = await questionsSubmit({
        id: this.list[this.currIndex].id,
        singleAnswer: this.ans1,
        multipleAnswer: this.ans2
      })
      this.$toast.clear()
      this.list[this.currIndex].ans1 = this.ans1
      this.list[this.currIndex].ans2 = this.ans2
      this.list[this.currIndex].ans3 = this.ans3
      // 静默刷新
      this.list[this.currIndex].answer = res.data.data
      // 提交时判断 是否最后一题step = 4：显示结束
      if (this.currIndex === this.list.length - 1) {
        this.step = 4
      } else {
        this.step = 3
      }
      this.bol = false
      this.$nextTick(() => {
        this.bol = false
      })
      // window.console.log(this.list)
    },
    // 下一题
    async next () {
      this.currIndex++
      // 获取题目
      this.$toast.loading({ duration: 0 })
      const res = await questionsId(this.list[this.currIndex].id)
      this.$toast.clear()
      this.list[this.currIndex].detail = res.data.data
      this.ans1 = ''
      this.ans2 = []
      this.ans3 = ''
      this.step = 1
      // window.console.log(this.list)
    }
  },
  async created () {
    const res = await interViewQuestions({
      type: this.id,
      city: this.city
    })
    // window.console.log(res)
    this.list = res.data.data
  }
}
</script>

<style lang="less" scoped>
.content {
  padding: 0 @p15;
  .title {
    opacity: 1;
    font-size: 16px;
    font-family: PingFangSC, PingFangSC-Regular;
    font-weight: 400;
    text-align: justify;
    color: #181a39;
    line-height: 22px;
  }
  .tag {
    padding: 5px 8px;
    margin-right: 15px;
  }
  .select {
    margin-top: 30px;
    .select-item {
      padding: 14px @p15;
      font-size: 16px;
      border: 1px solid #f7f4f5;
      font-family: PingFangSC, PingFangSC-Regular;
      font-weight: 400;
      text-align: justify;
      color: #181a39;
      margin-bottom: 16px;
    }
    .pass {
      background-color: #ddfad9;
      position: relative;
      &::after {
        content: '\e604';
        position: absolute;
        right: 10px;
        top: 50%;
        color: #45d18c;
        transform: translateY(-50%);
      }
    }
    // 错误
    .error {
      background-color: #ffefea;
      position: relative;
      &::after {
        content: '\e605';
        position: absolute;
        right: 10px;
        top: 50%;
        color: #ff4949;
        transform: translateY(-50%);
      }
    }
    // 定义一个选中后效果的class
    .active {
      border: 1px solid #00b8d4;
    }
  }
  .field {
    border: 2px solid #ccc;
  }
  // 答案解析
  .answer {
    padding-top: 51px;
    .answer-title {
      opacity: 1;
      font-size: 18px;
      font-family: PingFangSC, PingFangSC-Medium;
      font-weight: 500;
      text-align: left;
      color: #222222;
      line-height: 25px;
      letter-spacing: 0px;
    }
    .right-answer {
      padding: 15px 0;
      opacity: 1;
      font-size: 16px;
      font-family: PingFangSC, PingFangSC-Regular;
      font-weight: 400;
      text-align: justify;
      color: #1dc779;
      line-height: 22.5px;
    }
  }
  .other {
    background-color: #f7f4f5;
    padding: 10px;
    display: flex;
    font-size: 12px;
    justify-content: space-between;
  }
  .txt {
    opacity: 1;
    font-size: 16px;
    font-family: PingFangSC, PingFangSC-Regular;
    font-weight: 400;
    text-align: justify;
    color: #181a39;
    line-height: 22.5px;
    padding-bottom: 80px;
    padding-top: 30px;
  }
}
.bottom {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
  height: 60px;
  padding: 0 @p15;
  display: flex;
  align-items: center;
  text-align: center;
  background-color: #fff;
  .b1 {
    flex: 1;
    color: #ccc;
    .iconfont {
      font-size: 28px;
    }
    .txt {
      opacity: 1;
      font-size: 12px;
      font-family: PingFangSC, PingFangSC-Regular;
      font-weight: 400;
      color: #b4b4bd;
      line-height: 16px;
      letter-spacing: 0px;
    }
  }
  .b2 {
    flex: 2;
    span {
      opacity: 1;
      font-size: 22px;
      font-family: PingFangSC, PingFangSC-Regular;
      font-weight: 400;
      color: #e40137;
      line-height: 31px;
      letter-spacing: 1px;
    }
    font-size: 16px;
    font-family: PingFangSC, PingFangSC-Regular;
    font-weight: 400;
    color: #545671;
    line-height: 31px;
    letter-spacing: 1px;
  }
  .b3 {
    flex: 2;
    height: 40px;
    background-color: red;
    color: #fff;
  }
}
.card {
  height: 500px;
  display: flex;
  flex-wrap: wrap;
  padding: 0 @p15;
  justify-content: space-between;
  .card-item {
    width: 33px;
    height: 33px;
    border-radius: 50%;
    font-size: 12px;
    text-align: center;
    line-height: 33px;
    margin: 10px;
    background-color: #ccc;
  }
  .placeholder {
    width: 33px;
    height: 0px;
    border-radius: 50%;
    font-size: 12px;
    text-align: center;
    line-height: 33px;
    margin: 10px;
    background-color: #ccc;
  }
  .card-pass {
    background-color: #ddfad9;
    color: #84e2ac;
  }
  .card-error {
    background-color: #ffefea;
    color: #ff5a5a;
  }
}
</style>
